package com.linkstec.file;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Raftest {
	private static Logger logger = LoggerFactory.getLogger(Raftest.class);

	public static void main(String[] args) {
		RandomAccessFile raf = null;
		try {
//			raf = new RandomAccessFile("D:\\workspace_mars2_basic\\ZTest\\src\\main\\resources\\RandomAccessFile.txt", "rw");
			raf = new RandomAccessFile("D:\\workspace_mars2_basic\\log4j2test\\logs\\TradeReturn.log", "rw");
			
//			raf.seek(20);
			
			read(raf);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				raf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static void read(RandomAccessFile raf) {
		String ss = null;
		while (true) {
			try {
				ss = raf.readLine();
				if (ss != null){
					long filePointer = raf.getFilePointer();
					logger.info("当前位置是：" + filePointer);
					logger.info(new String(ss.getBytes("ISO-8859-1"),"utf-8"));
				}
				long filePointer = raf.getFilePointer();
//			long length = raf.length();
//			logger.info("文件大小：" + length);
				if (null == ss){
					try {
						Thread.sleep(3000);
						logger.info("当前位置也是最后位置，休眠一会继续读" + filePointer + "文件大小：" + raf.length());
						if (filePointer > raf.length()) {
							raf.seek(0);
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}